/*
 * Copyright (c) 2018, apexes.net. All rights reserved.
 *
 *         http://www.apexes.net
 *
 */
package net.apexes.commons.i18n;

import net.apexes.commons.lang.Checks;

import java.util.Locale;

/**
 * 
 * @author <a href="mailto:hedyn@foxmail.com">HeDYn</a>
 *
 */
public class I18Ns {

    public static Locale toLocale(String localeStr) {
        Checks.verifyNotEmpty(localeStr, "localeStr");
        int idx = localeStr.indexOf('_');
        if (idx == -1) {
            return new Locale(localeStr);
        }
        String language = localeStr.substring(0, idx);
        String country = localeStr.substring(idx + 1);
        return new Locale(language.toLowerCase(), country.toUpperCase());
    }

    public static I18nMsg msg(String bundleName, Locale locale) {
        return new I18nMsg(i18nResource(bundleName, locale));
    }

    public static I18nMsg msg(String bundleName, Locale locale, I18nMsg.Logger logger) {
        return new I18nMsg(i18nResource(bundleName, locale), logger);
    }

    public static EnumMsg enumMsg(String bundleName, Locale locale) {
        return new EnumMsg(i18nResource(bundleName, locale));
    }

    public static EnumMsg enumMsg(String bundleName, Locale locale, I18nMsg.Logger logger) {
        return new EnumMsg(i18nResource(bundleName, locale), logger);
    }

    public static I18nResource i18nResource(String bundleName, Locale locale) {
        return new JdkI18nResource(bundleName, locale);
    }

}
